package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.hjxh.HjxhData;
import com.ucredit.babel.common.model.third.hjxh.HjxhWholeInRecord;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 * 中互金在网时长
 *
 * @author renzhanzheng
 */

public class HjxhWholeInParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 中互金在网时长
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        HjxhWholeInRecord obj = (HjxhWholeInRecord)model;
        HjxhData data = obj.getData();
        JSONObject info = data.getData() == null ? new JSONObject() : JSON.parseObject(data.getData());

        StringBuilder sb = getBaseInfo(obj, successful);
        //状态码
        sb.append(info.getString("code") == null ? "" : info.getString("code")).append(CRT001);
        //请求id
        sb.append(info.getString("requestId") == null ? "" : info.getString("requestId")).append(CRT001);
        //查验结果
        sb.append(info.getString("result") == null ? "" : info.getString("result")).append(CRT001);
        //具体时长
        sb.append(info.getString("message") == null ? "" : info.getString("message")).append(CRT001);
        StringBuilder txSb = getBaseInfo(obj);
        key.set(getKey());
        val.set((sb.toString() + txSb.toString()).replace("\n", " ").replace("\r", " "));
        context.write(key, val);
        return true;
    }

    @Override
    public String getTableName() {
        return "kafka_babel_hujin_onlinetime_inc";
    }

    @Override
    public String getType() {
        return RecordType.HJXH_WHOLEIN_RECORD.toString();
    }

    @Override
    public String getComment() {
        return "中互金在网时长";
    }
}
